from django.contrib import admin
from app.models import PatientProfile, Title, UserProfile, EmployeeProfile, PatientAppointment, Prescription, Medication
from django.contrib.auth.models import Group
from django.contrib.sites.models import Site

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User, Permission


# define the UserProfile
class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = True
    verbose_name = 'Base Profile'
    max_num = 1

class EmployeeProfileInline(admin.TabularInline):
    model = EmployeeProfile
    can_delete = True
    verbose_name = 'Employee Profile'
    max_num = 1
        
class PatientAppointmentInline(admin.TabularInline):
    model = PatientAppointment
    can_delete = True;
    verbose_name = "Appointment"
    max_num = 100
    extra = 0
    
class PrescriptionInline(admin.TabularInline):
    model = Prescription
    can_delete = True;
    verbose_name = "Prescription"
    max_num = 100
    extra = 0

class PatientAppointmentAdmin(admin.ModelAdmin):
    inline = PatientAppointmentInline

class EmployeeProfileAdmin(admin.ModelAdmin):
    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ['user']
        else:
            return []

class PatientProfileAdmin(admin.ModelAdmin):
    inlines = (PatientAppointmentInline,PrescriptionInline)
    def get_readonly_fields(self, request, obj=None):
        if obj:
            return ['user']
        else:
            return []
    
class PatientProfileInline(admin.TabularInline):
    model = PatientProfile
    can_delete = True
    verbose_name = 'Patient Profile'
    max_num = 1
     
class BasicUserAdmin(UserAdmin):
    inlines = (UserProfileInline,)
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        #(('Personal Information'), {'fields': ('first_name', 'last_name', 'email')}),
        (('Roles'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups')}),
        (('Important dates'), {'fields': ('last_login', 'date_joined')})
        )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2', 'first_name', 'last_name', 'email')}
        ),
    )
    def queryset(self,request):
        qs = admin.ModelAdmin.queryset(self,request)
        if request.user.is_superuser:
            return qs
        # Only allow viewing/editing users who are not superusers
        return qs.filter(is_superuser=False)
    def get_readonly_fields(self, request, obj=None):
        # Deny editing groups, permissions and the superuser status
        return () if request.user.is_superuser else ('is_superuser', 'groups', 'user_permissions', 'last_login', 'date_joined', )



def userExists(username):
    if User.objects.filter(username=username).count():
        return True
    return False

admin.site.unregister(User)
admin.site.unregister(Site)
admin.site.unregister(Group)
admin.site.register(Permission)
admin.site.register(Medication)
admin.site.register(PatientProfile, PatientProfileAdmin)
admin.site.register(EmployeeProfile, EmployeeProfileAdmin)
admin.site.register(User, BasicUserAdmin)

admin.site.register(Group)
admin.site.register(Title)

